package com.ks.pc.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ks.DictEnum;
import com.ks.dto.BrandDto;
import com.ks.dto.ClassifyDto;
import com.ks.dto.Product2Dto;
import com.ks.dto.UserInfoDto;
import com.ks.pc.dao.DaoSupport;
import com.ks.pc.service.CommonService;
import com.ks.pc.service.PcService;

import net.chinahrd.utils.CollectionKit;

@Service("pcService")
public class PcServiceImpl extends BaseServiceImpl implements PcService {

	@Autowired
	private DaoSupport daoSupport;

	@Autowired
	private CommonService commonService;

	@Override
	public List<Product2Dto> queryPcHomeData() {
		try {
			return daoSupport.findForList("PcMapper.queryPcHomeData", null);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(e.getMessage());
		}
		return CollectionKit.newList();
	}

	@Override
	public List<Map<String, Object>> queryNavigationLeftData() {
		List<Map<String, Object>> rsMap = CollectionKit.newList();
		try {
			List<ClassifyDto> classifys = commonService.queryClassifyAll();
			for (ClassifyDto classifyDto : classifys) {
				if (!(StringUtils.equals(DictEnum.SHOU_YE.getInfo(), classifyDto.getName()))) {
					Integer classifyId = classifyDto.getId();
					Map<String, Object> map = CollectionKit.newMap();
					map.put("classifyId", classifyId);
					map.put("classifyName", classifyDto.getName());
					List<BrandDto> brandDtos = commonService.queryBrandAllByClassifyId(classifyId);
					if (CollectionKit.isNotEmpty(brandDtos)) {
						map.put("brandDtos", brandDtos);
					}
					rsMap.add(map);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return rsMap;
	}

	@Override
	public boolean saveOnlineOpt(UserInfoDto dto) {
		try {
			daoSupport.save("PcMapper.saveOnlineOpt", dto);
			return true;
		} catch (Exception e) {
			logger.debug(e.getMessage());
		}
		return false;
	}


}
